package controller;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import studysystem.db.MySQLConnector;
import application.Main;

public class MainScreenController  implements Initializable{
	
	private class Item {
		public int id;
		public String s, filename;

		Item(int id, String s, String filename) {
			this.id = id;
			this.s = s;
			this.filename = filename;
		}

		Item(String s) {
			this(-1, s, null);
		}

		public String toString() {
			return this.s;
		}
	}

	@FXML
	private Label txtusername;
	@FXML
	private Label teachername;
	@FXML
	private Button dotest;
	@FXML
	private ListView <Item> chapterlist;
	@FXML
	private Label coursepassed;
	
	public void initialize(URL arg0, ResourceBundle arg1) {	
		System.out.print("1"+Main.id);
	txtusername.setText(Main.name);
	
	 try {
         MySQLConnector conn = new MySQLConnector();
         ResultSet a=conn.execSQLQuery("SELECT * FROM TEACHER");
         if (a.next()){
         teachername.setText(a.getString("name"));}
	 }
	 catch (Exception e) {
         e.printStackTrace();
 } 
	 
	 
	 
	 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//////////////////CONNECTED TO CHAPTER LIST///////////////////////////////////////////////////////////////////////////
	 /////////////////////////////////////////////////////////////////////////////////////////////////////////////
	 
	 ObservableList<Item> chapterobslist=FXCollections.observableArrayList();
	 
	 
	 
	 
	 //viewing list of chapters and lectures
	 
	 try {
         MySQLConnector conn = new MySQLConnector();
         System.out.println();
         System.out.println(Main.id);
         ResultSet a=conn.execSQLQuery
        		 ("SELECT * FROM CHAPTER WHERE CHAPTER.id in (Select chapter_id from available_chapters where available_chapters.user_id='"+Main.id+"')");
         while (a.next()){ 
        	 
        	 //viewing chapter
        	 chapterobslist.add(new Item((a.getString("index")).toString().concat(" :  ").concat(a.getString("title"))));
         
         ResultSet b=conn.execSQLQuery("SELECT * FROM LECTURE WHERE chapter_id='"+a.getString("id")+"'");
         while (b.next()){
        	 //viewing lecture
         chapterobslist.add(new Item(b.getInt("id"), "         ".concat(b.getString("title")), b.getString("filename")));
         
         
         }
         }
	 }
	 catch (Exception e) {
         e.printStackTrace();
 } 
	 
	 chapterlist.setItems(chapterobslist);
		chapterlist.setOnMouseClicked(new EventHandler<MouseEvent>() {

			public void handle(MouseEvent arg0) {
				Item item = chapterlist.getSelectionModel().getSelectedItem();
				if (item.id != -1) {
					String path = "res/lec/";
					String filename = path + item.filename;
					File lec = new File(filename);
					if (lec.exists()) {
						if (Desktop.isDesktopSupported()) {
							try {
								Desktop.getDesktop().open(lec);
							} catch (IOException e) {
								e.printStackTrace();
							}
						} else {
							System.out.println("Awt Desktop is not supported!");
						}
					} else {
						System.out.println("!lec.exists()");
						//	 todo
					}
				}
			}

		});
	 
	 
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	 
	 
		
		
		int i=1;
			try {
				 MySQLConnector conn = new MySQLConnector();
		            
				ResultSet users = conn.execSQLQuery("SELECT Available_chapters.CHAPTER_ID FROM Available_chapters where user_id="+Main.id);
				 while (users.next()) {if (users.getInt("chapter_id")>i) {i=users.getInt("chapter_id");
				 }}
				 
			if (i>11)
			{
				coursepassed.setVisible(true);
				dotest.setVisible(false);
			}} catch (Exception e) {
					// TODO: handle exception
				}
				
		
		
		
	 
	}
	

	
	public void entertest(ActionEvent event)
	{
		int i=1;
		 Parent root;
			try {
				 MySQLConnector conn = new MySQLConnector();
		            
				ResultSet users = conn.execSQLQuery("SELECT Available_chapters.CHAPTER_ID FROM Available_chapters where user_id="+Main.id);
				 while (users.next()) {if (users.getInt("chapter_id")>i) {i=users.getInt("chapter_id");}};
				
				root = FXMLLoader.load(getClass().getResource("/application/Test"+i+".fxml"));
				Scene scene = new Scene(root); 
				//stage= new Stage();
				Stage stage = (Stage) dotest.getScene().getWindow();
				stage.setScene(scene);
				stage.show();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				
		
	}

}
